import { eachNext } from '../../utils/index.js'
import { _mid, _dis, _polar, _atan, _k, _sin } from '../index.js'
import { _mirror } from '../mirror.js'

// 计算控制点
export const calc = (p1, p2, {
    radiusRatio = 1,
    angleOffset = 0,
    orient  //向心
} = {}) => {
    let mid = _mid(p1, p2)
    let r = _dis(p1, mid)
    let cp1 = _polar(mid, radiusRatio * r, orient ? _atan(p1, p2) - 90 + angleOffset : angleOffset)
    let cp2 = _mirror(cp1, mid)
    return {
        p1,
        p2,
        cp1,
        cp2,
        mid,
        r
    }
}


// 曲线
export const curvePoints = (points, props) => {
    let ps = []
    let cs = []
    eachNext(points, (t, index, next) => {
        let p = calc(t, next, props)
        cs[cs.length] = p.cp1
        cs[cs.length] = p.cp2

    })
    eachNext(points, (t, index, next) => {
        ps[ps.length] = t
        ps[ps.length] = [...cs[2 * index], ...next]
    })
    return ps
}

// 波形
export const wavePoints = (points, props) => {
    let ps = []
    let cps = []
    let mids = []
    eachNext(points, (t, index, next) => {
        let p = calc(t, next, props)
        cps[cps.length] = p.cp1
        cps[cps.length] = p.cp2
        mids[mids.length] = p.mid

    })
    eachNext(points, (t, index, next) => {
        ps[ps.length] = t
        ps[ps.length] = [...cps[2 * index], ...mids[index]]
        ps[ps.length] = [...cps[2 * index + 1], ...next]
    })
    return ps
}


// 锯齿形
export const sawtoothPoints = (points, props) => {
    let ps = []
    let cps = []
    eachNext(points, (t, index, next) => {
        let p = calc(t, next, props)
        cps[cps.length] = p.cp1
        cps[cps.length] = p.cp2

    })
    eachNext(points, (t, index, next) => {
        ps[ps.length] = t
        ps[ps.length] = cps[2 * index]
        ps[ps.length] = cps[2 * index + 1]
        ps[ps.length] = next
    })
    return ps
}


// 半圆弧
export const semicirclePoints = (points, {
    sweepFlag,
    xAxisRotation = 0,
    largeArcFlag
} = {}) => {
    // A rx ry x-axis-rotation large-arc-flag sweep-flag x y
    let ps = []
    eachNext(points, (t, index, next) => {
        let r = _dis(t, next) / 2
        ps[ps.length] = [t, [r, r, xAxisRotation, largeArcFlag ? 1 : 0, sweepFlag ? 1 : 0, ...next]]
    })
    return ps
}

// 椭圆弧 elliptical arc 椭圆弧
export const ellipticalPoints = (points, {
    sweepFlag,
    radiusRatio = 1,
    xAxisRotation = 0,
    largeArcFlag
} = {}) => {
    // A rx ry x-axis-rotation large-arc-flag sweep-flag x y
    let ps = []
    // let n = _recycle(points, recycleIndex)
    eachNext(points, (t, index, next) => {
        let r = _dis(t, next) / 2
        ps[ps.length] = t
        ps[ps.length] = [r, radiusRatio * r, xAxisRotation, largeArcFlag ? 1 : 0, sweepFlag ? 1 : 0, ...next]
    })
    return ps
}

